<template>
  <q-page class="cc-admin">
    <div class="col bg-white shadow-2 q-pa-md q-ma-sm">
      <q-table flat color="primary" class="cross_table" separator="cell" :columns="columns" :data="list" row-key="id" :pagination.sync="pagination" :visible-columns="group" @request="query" :rows-per-page-options="[10, 20, 50, 100]" selection="multiple" :selected.sync="selected">
        <template v-slot:top="">
          <div class="row no-wrap full-width">
            <q-input clearable outlined dense placeholder="请输入关键字搜索" class="on-left" @input="query" debounce="500" v-model="key">
              <template #append>
                <q-btn flat round icon="search" color="primary" @click="query" :loading="loading">
                  <q-tooltip>搜索</q-tooltip>
                </q-btn>
              </template>
            </q-input>
            <q-space />
            <div class="q-gutter-xs" style="margin-left: 10px">
              <q-btn icon="mdi-file-search-outline" no-wrap color="primary" label="查看错题分布" @click="viewEcharts" />
            </div>
            <!-- <div class="q-gutter-xs">
              <q-btn icon="add" no-wrap color="primary" label="新建" @click="add" />
            </div> -->
          </div>
        </template>
        <template #body-cell-content="props">
          <q-td key="content" :props="props" style="width: 500px">
            <div style="width: 500px" class="cc-word-wrap">{{ props.row.content }}</div>
          </q-td>
        </template>
        <!-- <template #body-cell-questionStem="props">
          <q-td :props="props" style="max-width: 800px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis">{{ props.row.questionStem }}</q-td>
        </template> -->
        <template #body-cell-questionStem="props">
          <q-td :props="props" :auto-width="true" style="min-width: 800px; max-width: 800px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis">
            {{ props.row.questionStem }}
          </q-td>
        </template>
        <template #body-cell-opt="props">
          <q-td :props="props" :auto-width="true">
            <q-btn flat round dense color="primary" icon="edit" @click="edit(props.row)"> <q-tooltip>查看</q-tooltip></q-btn>
            <!-- <q-btn flat round dense color="primary" icon="mdi-content-copy" @click="copy(props.row)">
              <q-tooltip>复制</q-tooltip>
            </q-btn> -->
            <btn-del label="简答题" @confirm="del(props.row)" />
          </q-td>
        </template>
      </q-table>
    </div>
    <q-dialog maximized flat persistent ref="dialog">
      <q-form class="dialog_card column">
        <h5 class="view_title justify-between q-px-md">
          {{ '简答题错题' }}
          <q-btn dense outline round icon="clear" size="sm" v-close-popup />
        </h5>
        <q-scroll-area class="col">
          <div class="row q-col-gutter-x-md dialog_form q-pa-xl">
            {{ form.questionStem }}
            <div class="col-12" style="height: 100%">
              <q-splitter v-model="splitterModel" style="height: 250px">
                <template v-slot:before>
                  <q-tabs v-model="tab" vertical class="text-teal">
                    <q-tab :name="item.indexNum" icon="" :label="`第${index + 1}题`" v-for="(item, index) in form.questionList" :key="item.indexNum" @click="log1" />
                  </q-tabs>
                </template>

                <template v-slot:after>
                  <q-tab-panels v-model="tab" animated swipeable vertical transition-prev="jump-up" transition-next="jump-up">
                    <q-tab-panel :name="item.indexNum" v-for="item in form.questionList" :key="item.indexNum">
                      <div>
                        <div class="col-12">
                          <h5>题目：</h5>
                          {{ item.question }}
                        </div>
                        <div class="col-12">
                          <h5>你的作答：</h5>
                          {{ item.answer }}
                          <viewer class="label-list q-mb-sm" style="max-height: 100px" ref="viewer" :options="options" :image="item.viedoUrl" v-if="item.viedoUrl" @inited="inited">
                            <img class="q-pl-sm" :src="item.viedoUrl" style="object-fit: contain; max-height: 100%; max-width: 150px" />
                          </viewer>
                        </div>
                        <div class="col-12">
                          <h5>答案：</h5>
                          {{ item.realAnswer }}
                        </div>
                        <div class="col-12">
                          <h5>解析：</h5>
                          {{ item.analysis }}
                        </div>
                        <div style="display: flex" class="col-12">
                          <div class="q-video" style="margin-top: 10px" v-if="item.analysisVideo && imageExtensions.includes(item.analysisVideo.substring(item.analysisVideo.lastIndexOf('.')))">
                            <viewer class="label-list q-mb-sm" style="max-height: 100px" ref="viewer" :options="options" :image="item.analysisVideo" v-if="item.analysisVideo" @inited="inited">
                              <img class="q-pl-sm" :src="item.analysisVideo" style="object-fit: contain; max-height: 100%; max-width: 150px" />
                            </viewer>
                          </div>
                          <div class="q-video" style="margin-top: 10px" v-else>
                            <iframe :src="item.analysisVideo" frameborder="0" allowfullscreen height="100%" :ratio="4 / 3" />
                          </div>
                        </div>
                      </div>
                    </q-tab-panel>
                  </q-tab-panels>
                </template>
              </q-splitter>
            </div>
            <!-- 答案解析条件需要改为作答完毕 -->
            <h5 v-if="editType === '答题'" class="text-red-6">{{ item.analysis }}</h5>
          </div>
        </q-scroll-area>
        <div class="row justify-center q-pa-md">
          <q-btn outline color="primary" icon="mdi-close-thick" label="关闭" v-close-popup @click="reset" />
          <!-- <q-btn class="q-mx-md" color="primary" icon="mdi-check-bold" label="提交" :loading="loading" type="submit" /> -->
        </div>
      </q-form>
    </q-dialog>
    <q-dialog maximized flat persistent ref="EchartsDialog">
      <q-form class="dialog_card column">
        <h5 class="view_title justify-between q-px-md">
          错题分布
          <q-btn dense outline round icon="clear" size="sm" v-close-popup />
        </h5>
        <q-scroll-area class="col">
          <echartsBar ref="echartsBarview"></echartsBar>
        </q-scroll-area>
        <div class="row justify-center q-pa-md">
          <q-btn outline color="primary" icon="mdi-close-thick" label="关闭" v-close-popup />
        </div>
      </q-form>
    </q-dialog>
    <confirm ref="confirmDialog" :msg="confirmMsg" @confirm="deleteBatch()" />
    <q-inner-loading :showing="loading">
      <q-spinner-gears size="50px" color="primary" />
    </q-inner-loading>
  </q-page>
</template>

<script>
import { IndexMixin } from 'boot/mixins';
import { getDictLabel } from 'boot/dictionary';
import confirm from 'components/confirm';
import echartsBar from 'src/pages/postgraduate/echarts/echarts-bar.vue';
import Viewer from 'viewerjs';

export default {
  name: 'postDesignErrorQuestion',
  mixins: [IndexMixin],
  components: {
    confirm,
    echartsBar,
  },
  data() {
    return {
      catalog: '01',

      index: 0,
      tab: 0,
      splitterModel: 20,
      categoryOptions: [
        { label: '数据结构', value: '0101' },
        { label: '计算机组成原理', value: '0102' },
        { label: '操作系统', value: '0103' },
        { label: '计算机网络', value: '0104' },
      ],

      fileMd5: '',
      showVideo: false,
      fileResult: '',
      headers: [
        {
          name: 'Authorization',
          value: localStorage.getItem('Authorization'), // 从localStorage中正确获取值
        },
      ],
      form: {
        questionStem: '',
        categorys: [],
        subjectId: '',
        subjectText: '',
        majorId: '',
        majorText: '',
        questionList: [
          {
            indexNum: 0, // 题号
            subjectId: '',
            subjectText: '',
            majorId: '',
            majorText: '',
            categorys: [], // 分类
            categoryIds: '',
            categoryIdsText: '',
            question: '',
            answer: '',
            realAnswer: '',
            analysis: '',
            viedoUrl: '',
          },
        ],
      },
      columns: [
        {
          name: 'index',
          align: 'center',
          label: '序号',
          field: 'index',
          // headerStyle: 'max-width:50px;min-width:50px;',
          // style: 'max-width:50px;min-width:40px;',
        },
        {
          name: 'majorText',
          align: 'left',
          label: '科目',
          field: 'majorText',
        },
        {
          name: 'questionStem',
          align: 'left',
          label: '题目',
          field: 'questionStem',
        },
        {
          name: 'opt',
          align: 'center',
          label: '操作',
          field: 'id',
        },
      ],
      showQuery: true,
      // uploadUrl: `${process.env.SERVER_URL}${process.env.BASE_URL}/sys/common/upload`,
      uploadUrl: `${process.env.SERVER_URL}${process.env.BASE_URL}/upload`,
      imgUrl: `${process.env.SERVER_URL}${process.env.BASE_URL}/sys/common/static`,
      url: {
        list: '/design/questionsList/getErrorDesign',
        add: '/design/questionsList/add',
        edit: '/design/questionsList/edit',
        delete: '/design/questionsList/deleteAnswer',
        deleteBatch: '/design/questionsList/deleteAnswerBatch',
      },
    };
  },
  methods: {
    inited(viewer) {
      this.$viewer = viewer;
    },
    viewEcharts() {
      this.$refs.EchartsDialog.show();
      this.$nextTick(() => {
        this.$refs.echartsBarview.show(this.list);
      });
    },
    query(props) {
      if (!this.beforeQuery()) {
        return false;
      }
      if (props && props.pagination) {
        this.pagination = props.pagination;
      }
      this.loading = true;
      return this.$axios
        .get(this.url.list, {
          params: {
            ...this.queryParam(),
            ...this.searchForm,
            key: this.key,
            catalog: this.catalog,
            pageNo: this.pagination.page,
            pageSize: this.pagination.rowsPerPage,
          },
        })
        .then((r) => {
          if (!r.result || !r.result || r.result.length === 0) {
            this.list = [];
            this.pagination.rowsNumber = 0;
          } else {
            this.list = r.result.map((v, i) => {
              if (r.result.current > 1) {
                v.index = (r.result.current - 1) * r.result.size + (i + 1);
              } else {
                v.index = i + 1;
              }
              return v;
            });
            this.pagination.rowsNumber = r.result.total;
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },

    // 使用JSON.parse和JSON.stringify
    reset() {
      this.index = 0;
      this.tab = 0;
      this.form = JSON.parse(JSON.stringify(this.emptyForm));
    },
    log1() {
      console.log(this.form.questionList);
    },
    startUpload(index) {
      console.log(index);
      this.index = index;
    },

    calcDataIndex(dataList) {
      if (dataList && dataList.length > 0) {
        dataList.map((v, i) => {
          v.indexNum = i + 1;
          return v;
        });
      }
    },
    delSub(dataList) {
      if (dataList && dataList.length > 0) {
        dataList.pop();
      }
    },
    getDictLabel,
    initDict() {},
    uploadResult(info) {
      const fileResult = JSON.parse(info.xhr.response);
      this.fileResult = fileResult;
      console.log(this.index);
      this.form.questionList[this.index].viedoUrl = `http://127.0.0.1:9000${fileResult.url}`;

      this.showVideo = true;
    },
    delVideo() {
      console.log(this.form.questionList);
      console.log(this.index);
      this.$axios
        .post(`${process.env.SERVER_URL}${process.env.BASE_URL}/deleteVideo`, {
          url: this.form.questionList[this.index].viedoUrl,
        })
        .then((r) => {
          this.$info(r.module);
          this.form.questionList[this.index].viedoUrl = '';
        });
    },
  },
  mounted() {},
};
</script>

<style lang="stylus">
label
  span
    margin-top 10px
img
  width 100%
  height auto
  aspect-ratio 16/9
.q-splitter__before, .q-splitter__after
    display inline-table
</style>
